package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysTmBillDetail;
import com.settlement.system.mapper.SysTmBillDetailMapper;
import com.settlement.system.service.SysTmBillDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysTmBillDetailQuery;
import com.settlement.system.model.form.SysTmBillDetailForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysTmBillDetailImportVo;

/**
 * <p>
 * 天猫账单明细 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-04-15
 */
@Service
public class SysTmBillDetailServiceImpl extends ServiceImpl<SysTmBillDetailMapper, SysTmBillDetail> implements SysTmBillDetailService {

    @Override
    public Page<SysTmBillDetail> getPage(SysTmBillDetailQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysTmBillDetail> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysTmBillDetail(SysTmBillDetailForm sysTmBillDetailForm) {
         SysTmBillDetail sysTmBillDetail = new SysTmBillDetail();
         BeanUtil.copyProperties(sysTmBillDetailForm, sysTmBillDetail);
         sysTmBillDetail.setCreateTime(null);
         return this.saveOrUpdate(sysTmBillDetail);
     }

     @Override
     public SysTmBillDetailForm getSysTmBillDetailFormData(Long id) {
         SysTmBillDetail sysTmBillDetail = this.getById(id);
         SysTmBillDetailForm sysTmBillDetailForm = new SysTmBillDetailForm();
         BeanUtil.copyProperties(sysTmBillDetail, sysTmBillDetailForm);
         return sysTmBillDetailForm;
     }

     @Override
     public boolean deleteSysTmBillDetail(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysTmBillDetailImportVo> voList)  {
        List<SysTmBillDetail> sysTmBillDetailList = new ArrayList<>();
        for (SysTmBillDetailImportVo importVo : voList) {
            SysTmBillDetail sysTmBillDetail = new SysTmBillDetail();
            BeanUtil.copyProperties(importVo, sysTmBillDetail);
            sysTmBillDetailList.add(sysTmBillDetail);
        }
        this.saveOrUpdateBatch(sysTmBillDetailList);
    }

    @Override
    public List<SysTmBillDetailImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<SysTmBillDetailImportVo>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysTmBillDetail> sysTmBillDetailList = this.getBaseMapper().selectBatchIds(ids);
        List<SysTmBillDetailImportVo> voList = new ArrayList<>();
        for (SysTmBillDetail sysTmBillDetail : sysTmBillDetailList) {
            SysTmBillDetailImportVo vo = new SysTmBillDetailImportVo();
            BeanUtil.copyProperties(sysTmBillDetail, vo);
            voList.add(vo);
        }
        return voList;
    }
}
